package com.bt.mes.brm.provider.service;

import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_INQUIRE;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISBOM_NOT_FOUND_RECORDER;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISBOM_RECORDER_ALREADY_EXIST;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_MESG_OK;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.bt.mes.brm.api.service.IXpbisbomService;
import com.bt.mes.brm.provider.tx.Xpbisbom.XpbisbomI;
import com.bt.mes.brm.provider.tx.Xpbisbom.XpbisbomIA;
import com.bt.mes.brm.provider.tx.Xpbisbom.XpbisbomO;
import com.bt.mes.brm.provider.tx.Xpbisbom.XpbisbomOA;
import com.bt.mes.common.dao.IBisBomDao;
import com.bt.mes.common.dao.IBisDataDao;
import com.bt.mes.common.dao.IBisMdlDefDao;
import com.bt.mes.common.dao.IBisMtrlDao;
import com.bt.mes.common.util.GUIDGenerator;
import com.bt.mes.common.util.MqOpe;
import com.bt.table.ppt.Bis_bom;
import com.bt.table.ppt.Bis_bomId;
import com.bt.table.ppt.Bis_data;
import com.bt.table.ppt.Bis_mdl_def;
import com.bt.table.ppt.Bis_mtrl;

@Service("xpbisbomService")
public class XpbisbomService implements IXpbisbomService {

	private Logger logger = Logger.getLogger(XpbisbomService.class);
	@Autowired
	private XpbisbomI xpbisbomI;

	@Autowired
	private XpbisbomO xpbisbomO;

	@Autowired
	private IBisBomDao bisBomDao;

	@Autowired
	private IBisMtrlDao bisMtrlDao;

	@Autowired
	private IBisMdlDefDao bisMdlDefDao;

	@Autowired
	private IBisDataDao bisDataDao;

	@Autowired
	private MqOpe mqOpe;

	public String subMainProc(String strInMsg) {
		logger.info("InTrx is " + strInMsg);
		try {
			xpbisbomO.setTrx_id("XPBISBOM");
			xpbisbomO.setType_id("O");
			xpbisbomI = (XpbisbomI) mqOpe.inStr2Obj(strInMsg, xpbisbomI);
			char action_flg = xpbisbomI.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByConditionFnc();
				break;
			case ACTION_FLG_ADD:
				addFnc();
				break;
			case ACTION_FLG_UPDATE:
				updateFnc();
				break;
			case ACTION_FLG_DELETE:
				deleteFnc();
				break;
			case ACTION_FLG_INQUIRE:
				queryByMtrl();
				break;
			default:
				errActionFnc();
			}
			if (RETURN_CODE_OK.equals(xpbisbomO.getRtn_code())) {
				bisBomDao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			logger.info(mqOpe.obj2Str(xpbisbomO));
		}
		return mqOpe.obj2Str(xpbisbomO);
	}

	public void addFnc() {
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		Bis_bomId id = new Bis_bomId();

		String mtrl_prod_id_fk = iary.getMtrl_prod_id_fk();
		String mdl_id_fk = iary.getMdl_id_fk();

		id.setMdl_id_fk(mdl_id_fk);
		id.setMtrl_prod_id_fk(mtrl_prod_id_fk);
		id.setMdl_mtrl_seq_id(GUIDGenerator.javaGUID());

		Bis_bom queryBisQrs = bisBomDao.get(id);
		if (queryBisQrs != null) {
			xpbisbomO.setRtn_code(E_XPBISBOM_RECORDER_ALREADY_EXIST + "");
			xpbisbomO.setRtn_mesg("Record already Exist");
			return;
		}
		Bis_bom bis_bom = new Bis_bom();
		bis_bom.setId(id);
		bis_bom.setOpe_id_fk(iary.getOpe_id_fk());
		bis_bom.setMtrl_unit_fk(iary.getMtrl_unit_fk());
		bis_bom.setCns_flg(iary.getCns_flg());
		bis_bom.setMnt_flg(iary.getMnt_flg());
		bis_bom.setMtrl_use_qty(iary.getMtrl_use_qty());
		bisBomDao.save(bis_bom);

		Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id_fk);
		if (bis_mtrl == null) {
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg("找不到料号:" + mtrl_prod_id_fk);
			return;
		}
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdl_id_fk);
		if (bis_mdl_def == null) {
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg("找不到成品型号:" + mdl_id_fk);
			return;
		}
		bisMdlDefDao.update(bis_mdl_def);

		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg(RETURN_MESG_OK);
	}

	public void updateFnc() {
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		String mtrl_prod_id_fk = iary.getMtrl_prod_id_fk();
		String mdl_id_fk = iary.getMdl_id_fk();
		Bis_bomId id = new Bis_bomId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		id.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
		id.setMdl_mtrl_seq_id(iary.getMdl_mtrl_seq_id());
		Bis_bom bis_bom = bisBomDao.get(id);
		if (bis_bom == null) {
			xpbisbomO.setRtn_code(E_XPBISBOM_NOT_FOUND_RECORDER + "");
			xpbisbomO.setRtn_mesg("not Found Record[BIS_BOM]");
			return;
		}
		if (iary.getOpe_id_fk() != null) {
			bis_bom.setOpe_id_fk(iary.getOpe_id_fk());
		}
		if (iary.getMtrl_unit_fk() != null) {
			bis_bom.setMtrl_unit_fk(iary.getMtrl_unit_fk());
		}
		if (iary.getCns_flg() != null) {
			bis_bom.setCns_flg(iary.getCns_flg());
		}
		if (iary.getMnt_flg() != null) {
			bis_bom.setMnt_flg(iary.getMnt_flg());
		}
		if (iary.getMtrl_use_qty() != null) {
			bis_bom.setMtrl_use_qty(iary.getMtrl_use_qty());
		}
		bisBomDao.save(bis_bom);

		Bis_mtrl bis_mtrl = bisMtrlDao.get(mtrl_prod_id_fk);
		if (bis_mtrl == null) {
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg("找不到料号:" + mtrl_prod_id_fk);
			return;
		}
		Bis_mdl_def bis_mdl_def = bisMdlDefDao.get(mdl_id_fk);
		if (bis_mdl_def == null) {
			xpbisbomO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisbomO.setRtn_mesg("找不到成品型号:" + mdl_id_fk);
			return;
		}
		bisMdlDefDao.update(bis_mdl_def);

		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg(RETURN_MESG_OK);
		return;
	}

	public void deleteFnc() {
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		Bis_bomId id = new Bis_bomId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		id.setMtrl_prod_id_fk(iary.getMtrl_prod_id_fk());
		id.setMdl_mtrl_seq_id(iary.getMdl_mtrl_seq_id());
		Bis_bom bis_bom = bisBomDao.get(id);
		if (bis_bom == null) {
			xpbisbomO.setRtn_code(E_XPBISBOM_NOT_FOUND_RECORDER + "");
			xpbisbomO.setRtn_mesg("not Found Record[BIS_BOM]");
			return;
		}
		bisBomDao.delete(bis_bom);
		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg(RETURN_MESG_OK);
	}

	public String comObj2String(Object obj) {
		if (obj != null) {
			return obj.toString();
		}
		return null;
	}

	public XpbisbomO queryByConditionFnc() {
		/***
		 * 暂只支持 by mdl_id_fk 查询
		 **/

		List<XpbisbomOA> oaryList = new ArrayList<XpbisbomOA>();

		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		String mdlId = iary.getMdl_id_fk();

		String hql = "FROM Bis_bom where mdl_id_fk = ?";
		List<Bis_bom> bisBomList = bisBomDao.list(hql, mdlId);
		for (Bis_bom bisBom : bisBomList) {
			String mtrlProdId = bisBom.getId().getMtrl_prod_id_fk();
			Bis_mtrl bisMtrl = bisMtrlDao.get(mtrlProdId);
			XpbisbomOA oary = new XpbisbomOA();
			oary.setMdl_id_fk(mdlId);
			oary.setMtrl_prod_id_fk(bisMtrl.getMtrl_prod_id());
			oary.setMtrl_cate(mtrlProdId);
			oary.setOpe_id_fk(bisBom.getOpe_id_fk());
			oary.setMtrl_use_qty(bisBom.getMtrl_use_qty());
			oary.setCns_flg(bisBom.getCns_flg());
			oary.setMnt_flg(bisBom.getMnt_flg());
			oary.setMtrl_prod_dsc(bisMtrl.getMtrl_prod_dsc());
			oary.setMtrl_unit_fk(bisBom.getMtrl_unit_fk());
			oary.setMdl_mtrl_seq_id(bisBom.getId().getMdl_mtrl_seq_id());
			hql = "FROM Bis_data where data_cate = ? and ext_1 = ?";
			String mtrl_mkr = "";
			List<Bis_data> bisDataList = bisDataDao.list(hql, "CSMT", mtrlProdId);
			for (Bis_data bisData : bisDataList) {
				mtrl_mkr += bisData.getData_item();
				mtrl_mkr += (bisDataList.indexOf(bisData) == bisDataList.size() - 1) ? "" : ",";
			}
			oary.setMtrl_mkr(mtrl_mkr);
			oaryList.add(oary);
		}

		xpbisbomO.setOary(oaryList);
		xpbisbomO.setTbl_cnt(oaryList.size());
		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg(RETURN_MESG_OK);
		return xpbisbomO;
	}

	public void queryByMtrl() {
		List<XpbisbomOA> oaryList = new ArrayList<XpbisbomOA>();
		XpbisbomIA iary = xpbisbomI.getIary().get(0);
		String hql = "FROM Bis_bom where 1=1 ";
		String mdl_id_fk = iary.getMdl_id_fk();
		String mtrl_prod_id_fk = iary.getMtrl_prod_id_fk();
		if (mdl_id_fk != null) {
			hql = hql + " and  id.mdl_id_fk='" + mdl_id_fk + "'";
		}
		if (mtrl_prod_id_fk != null) {
			hql = hql + " and  id.mtrl_prod_id_fk='" + mtrl_prod_id_fk + "'";
		}
		List<Bis_bom> bisBomList = bisBomDao.find(hql);
		for (Bis_bom bis_bom : bisBomList) {
			XpbisbomOA oary = new XpbisbomOA();
			oary.setMdl_id_fk(bis_bom.getId().getMdl_id_fk());
			oary.setMtrl_prod_id_fk(bis_bom.getId().getMtrl_prod_id_fk());
			oary.setMdl_mtrl_seq_id(bis_bom.getId().getMdl_mtrl_seq_id());
			oary.setOpe_id_fk(bis_bom.getOpe_id_fk());
			oary.setMtrl_use_qty(bis_bom.getMtrl_use_qty());
			oary.setMtrl_unit_fk(bis_bom.getMtrl_unit_fk());
			oary.setMnt_flg(bis_bom.getMnt_flg());
			oary.setCns_flg(bis_bom.getCns_flg());
			oaryList.add(oary);
		}
		xpbisbomO.setOary(oaryList);
		xpbisbomO.setTbl_cnt(oaryList.size());
		xpbisbomO.setRtn_code(RETURN_CODE_OK);
		xpbisbomO.setRtn_mesg(RETURN_MESG_OK);
	}

	public XpbisbomO errActionFnc() {
		XpbisbomO xpbisbomO = new XpbisbomO();
		xpbisbomO.setTrx_id("XPLSTDAT");
		xpbisbomO.setType_id("O");
		xpbisbomO.setRtn_code("0000009");
		xpbisbomO.setRtn_mesg("Invalid Action_flg");
		return xpbisbomO;
	}

}
